package redsig.signature;


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import javax.swing.tree.DefaultMutableTreeNode;

import redsig.hash.Hasher;

import core.Adapter;


import data.XMLNode;

public class SignatureImpl {

	public static Signature checkForSignatureFile(File file) {
		String fileName = createSignatureFileName(file);
		File sigFile = new File(fileName);
		
		Signature signature = mapToSignature(sigFile);
		
		return signature;
	}

	private static Signature mapToSignature(File file) {
		Signature signature = null;
		String hashValue = "";
		String seed = "";
		boolean readingSignature = true;
		try {
			InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream (file));
			BufferedReader bufferedReader = new BufferedReader (inputStreamReader);
	
			String line;
			while((line = bufferedReader.readLine()) != null){
				if(!line.contains("Signature:")){
					if(line.contains("Seed:")){
						readingSignature = false;
					}
					else if(readingSignature){
						hashValue += line;
					}
					else if(!readingSignature){
						seed += line;
					}
						
				}
			}
			byte[] hashValueBytes = Adapter.base64ToByte(hashValue);
//			byte[] hashValueBytes = Adapter.hexToByte(hashValue);
			byte[] seedBytes = null;
			if(!seed.equals("")){
				seedBytes = Adapter.base64ToByte(seed); 
			}
			signature = new Signature(hashValueBytes, seedBytes);
			
			System.out.println("Signature value: " + Adapter.byteToBase64(signature.getSignature()));
		} catch (FileNotFoundException e) {
			return null;
		} catch (IOException e) {
			return null;
		}

		return signature;
	}

	private static String createSignatureFileName(File file) {
		String fileName = file.getAbsolutePath().replace(".xml", ".sig");
		System.out.println(fileName);
		return fileName;
	}

	public static boolean verifySignature(XMLNode xmlNode, DocumentSigner docSigner) {
		System.out.println("Verifying");
		byte[] salt = docSigner.getSignatureFile().getSalt();
		//System.out.println("Signature-Salt: " + Adapter.byteToBase64(salt));
		if(salt != null){
			hashUnredactedDocument(xmlNode, salt);
		}
		else{
			//hashRedactedDocument(xmlNode);
		}
		if(docSigner.verify(xmlNode.getHashValue()))
			return true;
		
		else return false;
		/*signer.sign(xmlNode.getHashValue());
		byte[] signatureValue = signer.getSig();
		
		
		if(compareSignatures(signatureValue, signature.getSignature())){
			System.out.println("Verified");
			return true;
		}
		else{
			System.out.println("not Verified");
			System.out.println("--------------");
			//System.out.println(Adapter.byteToBase64(signatureValue));
			return false;
		}
		*/
	}

	private static boolean compareSignatures(byte[] sig1, byte[] sig2) {
		if(sig1.length != sig2.length)
			return false;
		for(int i = 0; i < sig1.length; i++){
			if(sig1[i] != sig2[i]){
				return false;
			}
		}
		return true;
	}

	public static void hashUnredactedDocument(XMLNode xmlNode, byte[] salt) {
		try {
			Hasher hasher = new Hasher(xmlNode);
			hasher.salt(salt);
			hasher.hash();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

}
